package shop.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import shop.common.entity.Product;
import shop.product.mapper.ProductMapper;
import shop.product.service.ProductService;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {


    @Override
    public Page<Product> list(Product product, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Product> wrapper = Wrappers.lambdaQuery(Product.class)
                .eq(!ObjectUtils.isEmpty(product.getId()), Product::getId, product.getId())
                .like(!ObjectUtils.isEmpty(product.getName()), Product::getName, product.getName());

        Page<Product> page = new Page<>(pageNum, pageSize);
        this.page(page, wrapper);
        List<Product> list = page.getRecords().stream()
                .map(item -> this.details(item.getId()))
                .collect(Collectors.toList());
        page.setRecords(list);
        return page;
    }

    @Override
    public Product details(String id) {
        return baseMapper.selectById(id);

    }

    @Override
    public List<Product> listByName(String name) {
        List<Product> list = baseMapper.selectList(Wrappers.<Product>lambdaQuery().like(Product::getName, name));
        return list.stream().map(product ->
                this.details(product.getId())
        ).collect(Collectors.toList());
    }
}




